package com.sudichina.sudichina.view.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.bean.User;
import com.sudichina.sudichina.utils.GetSystem;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.utils.UiUtils;
import com.sudichina.sudichina.view.activity.main.BaseActivity;
import com.sudichina.sudichina.view.activity.main.MainActivity;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${杨伟乔} on 2017/3/8.
 * 邮箱: yangweiqiao@126.com
 * QQ: 463217558
 * 别人光鲜的背后或者有着太多不为人知的痛苦
 * 学习可以改变一切、 行动可以解决一切问题、
 */
public class QuickLoginActivity extends BaseActivity implements onRequestDataFinish {


    @BindView(R.id.iv_back)
    ImageView privateIvBack;
    @BindView(R.id.tv_title)
    TextView privateTvTitle;
    @BindView(R.id.tv_right)
    TextView privateTvRight;
    @BindView(R.id.et_phone_no)
    MaterialEditText privateEtPhoneNo;
    @BindView(R.id.et_identifying_code)
    MaterialEditText privateEtIdentifyingCode;
    @BindView(R.id.btn_get_identifying_code)
    Button privateBtnGetIdentifyingCode;
    @BindView(R.id.btn_login)
    Button privateBtnLogin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_quick_login);
        ButterKnife.bind(this);
        privateTvTitle.setText("快捷登录");

    }

    @Override
    public void requestdataFinish(String catalog, Object bean) {

        switch (catalog) {

            case Api.SEND_CODE:
                Result result = (Result) bean;

                int code = result.getCode();
                if (code == 1) {
                    UiUtils.showToast("验证码发送成功");
                }
                if (code == 8005) {
                    UiUtils.showToast("验证码发送失败");
                }


                break;


            case Api.EQUALS_CODE:

                Result coderesult = (Result) bean;
                int code1 = coderesult.getCode();
                if (code1 == 1 || coderesult.getMsg().equals("验证码通过！")) {
                    //成功后开始注册
                    HashMap map = new HashMap<String, Object>();
/**
 * " authTarget ": "17301063036",
 " authCode ": "123123"
 */
                    map.put("authTarget", phone);
                    map.put("authCode", privateEtIdentifyingCode.getText().toString().trim());

                    Type type = new TypeToken<Result<User>>() {
                    }.getType();
                    NetUtils.requestData(Api.QUICKLOGIN, map, this, type, false);
                }
                if (code1 == 8007) {
                    Toastutils.showToast("验证码失效");
                }


                break;
            case Api.QUICKLOGIN:
                Result<User> bean1 = (Result<User>) bean;
                int code3 = bean1.getCode();
                User user = bean1.getData();

                String msg = bean1.getMsg();


                if (code3 == 8007) {
                    Toastutils.showToast(msg);

                    return;
                }
                //保存为全局变量
                MyApp.setCurrentUser(user);
                System.out.println("登录成功");
                startActivity(new Intent(this, MainActivity.class));
                LoginActivity.context.finish();
                finish();


                break;

        }


    }


    @Override
    public void requestdataFailed(IOException e) {

    }

    String phone;

    @OnClick({R.id.iv_back, R.id.btn_get_identifying_code, R.id.btn_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_get_identifying_code:

                phone = privateEtPhoneNo.getText().toString().trim();

                if (GetSystem.isMobileNO(phone)) { //正则判断
                    /**
                     * 发送验证码
                     */
                    showGetVerificationCodeState();
                    sendCode();
                } else {
                    Toastutils.showToast("手机格式不正确");
                }

                break;
            case R.id.btn_login:
//效验验证码
                String code = privateEtIdentifyingCode.getText().toString().trim();
                HashMap<String, Object> map = new HashMap<>();
/*
    "authTarget":"15321859523",
                "authType":"sms_shortcut_login",
                "authCode":"011231"
 */
//                map.put("authTarget", phone);
//                map.put("authType", "sms_shortcut_login");
//                map.put("authCode", code);
//                Type type = new TypeToken<Result>() {
//                }.getType();
//                NetUtils.requestData(Api.EQUALS_CODE, map, this, type, false);
                map.put("authTarget", phone);
                map.put("authCode", privateEtIdentifyingCode.getText().toString().trim());

                Type type = new TypeToken<Result<User>>() {
                }.getType();
                NetUtils.requestData(Api.QUICKLOGIN, map, this, type, false);

                break;
        }
    }


    private void sendCode() {

        //  http://192.168.1.37:8888/services/sms/send

        HashMap<String, Object> map = new HashMap<>();
/*
"authTarget":"15321859523",
"authType":"sms_shortcut_login",
"authSource":"Android"
 */
        map.put("authTarget", phone);
        map.put("authType", "sms_shortcut_login");
        map.put("authSource", "Android");

        Type type = new TypeToken<Result>() {
        }.getType();
        NetUtils.requestData(Api.SEND_CODE, map, this, type, false);
    }


    private static final int TIME_INTERVAL = 60;
    private int one_minute = TIME_INTERVAL;
    private Handler handler = new Handler();

    public void showGetVerificationCodeState() {
        privateBtnGetIdentifyingCode.setEnabled(false);
        handler.post(new Runnable() {
            @Override
            public void run() {
                privateBtnGetIdentifyingCode.setText(--one_minute < 0 ? "获取验证码" :
                        String.valueOf(one_minute).concat("秒"));
                if (one_minute < 0) {
                    one_minute = TIME_INTERVAL;
                    privateBtnGetIdentifyingCode.setEnabled(true);
                    return;
                }
                handler.postDelayed(this, 1000);
            }
        });
    }
}
